/**
 * Handles everything from adding/deleting/replacing shapes
 * No drawing is done here
 */

function ShapeManager()
{
	/**
	 * Contains all the shapes ...
	 */
	this.data = [];
	
	/**
	 * Used for undo / redo
	 */
	this.backup = [];
	
	
	//=================================================================
	// Basic operations
	//=================================================================
	/**
	 * Return the last index of the data buffer
	 */
	this.lastIndex = function()
	{
		return this.data.length - 1;
	};
	
	/**
	 * Adds a Shape into the data buffer (see Shape.js)
	 * @param Shape
	 */
	this.addShape = function(type, color, thickness)
	{
		if (type == "Free"){
			this.data.push(new Shape(color, thickness));
		}
		else if (type == "Circle"){
			this.data.push(new Circle(color, thickness));
		}
		else if (type == "Line"){
			this.data.push(new Line(color, thickness));
		}
	};

	
	/**
	 * Removes a specific Shape
	 * @param index
	 */
	this.removeShape = function(index)
	{
		if (index != null)
			this.data.splice(index, 1);
	};
	
	/**
	 * Return a specific Shape
	 * @param index
	 */
	this.getShape = function(index)
	{
		if ((index < this.data.length) && (index >= 0))
			return this.data[index];
		else
			return null;
	};
	
	/**
	 * Return a specific Shape
	 * @param index
	 */
	this.getLastShape = function()
	{
		return this.data[this.data.length-1];
	};
	
	/**
	 * Swaps the order of 2 elements
	 * @param index1
	 * @param index2
	 */
	this.swapShapes = function(index1, index2)
	{
		var tmp = this.data[index1];
		
		this.data[index1] = index2;
		this.data[index2] = tmp;
	};
	
	/**
	 * Undo the last Shape
	 */
	this.undo = function()
	{
		if (this.data.length > 0)
		{
			console.log("Shape manager undo");
			this.backup.push(this.data[this.data.length-1]);
			this.data.pop();
		}
	};
	
	/**
	 * Redo the last Shape
	 */
	this.redo = function()
	{
		if (this.backup.length > 0)
		{
			this.data.push(this.backup[this.backup.length-1]);
			this.backup.pop();
		}
	};
	
	this.getData = function()
	{	
		return this.data;
	}
	
	this.fuseShapes = function(shapes){
		
		var k = 0;
		
		// There are shapes selected
		if (shapes.length > 1){
		
			var newShape = new Shape(this.data[shapes[0]].getColor(), 
									 this.data[shapes[0]].getThickness());
			
			for (var i = 0 ; i < shapes.length ; i++){
			
				
				// it's a fusioned shape
				if (this.data[shapes[i]].breakPoints.length > 0){
				
					k = 0;
					
					for(var j = 0 ; j < this.data[shapes[i]].breakPoints.length ; j++){
						for (k = k ; k < this.data[shapes[i]].breakPoints[j] ; k++){
							newShape.x.push(this.data[shapes[i]].x[k]);
							newShape.y.push(this.data[shapes[i]].y[k]);
							newShape.time.push(this.data[shapes[i]].time[k]);
						}
						
						newShape.breakPoints.push(newShape.x.length);
					}
					
				}
				else{
				
					newShape.x = newShape.x.concat(this.data[shapes[i]].x);
					newShape.y = newShape.y.concat(this.data[shapes[i]].y);
					newShape.time = newShape.time.concat(this.data[shapes[i]].time);
					
					newShape.breakPoints.push(newShape.x.length);
				}
			}
			
			shapes.reverse();
			for (var i = 0 ; i < shapes.length ; i++){
				this.removeShape(shapes[i]);
			}
			
			// Adding to cache
			newShape.addToCache(true);
			
			this.data.push(newShape);
		}
		
	}
	
	this.unFuseShapes = function(index){
		
		var shape = this.data[index];
		
		if (shape.breakPoints.length > 0){
		
			this.removeShape(index);
			
			var i,j=0;
			
			for(var i = 0 ; i < shape.breakPoints.length ; i++){
				
				// Creating the shape that will be added in the array
				var tmpShape = new Shape(shape.color, shape.thickness);
				
				for (j = j ; j < shape.breakPoints[i] ; j++){
					
					// Filling the shape
					tmpShape.x.push(shape.x[j]);
					tmpShape.y.push(shape.y[j]);
					tmpShape.time.push(shape.time[j]);
				}
				
				// Add to cache once it's finished
				tmpShape.addToCache(true);
				
				// Add it to the data
				this.data.push(tmpShape);
			}
		}
	}
	
	this.duplicateShape = function(index){
		
		this.data.push(this.data[index].clone());
	}
};